

#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <Reader/Reader.h>
#include <BLE/BLE.h>
#include <Projects/VersionM356.h>


#if 0
static const char FirmwareVerseionP[] = "TWN4/B1.08/NCF4.06/" NAME I_NUMBER "/" "P";
static const char FirmwareVerseionPI[] = "TWN4/B1.08/NCF4.06/" NAME I_NUMBER "/" "PI";
static const char FirmwareVerseionP0[] = "TWN4/B1.08/NCL4.06/" NAME P_NUMBER "/" "P";
static const char FirmwareVerseionPIB[] = "TWN4/B1.08/NCF4.06/" NAME I_NUMBER "/" "PIB";
#endif 

static const char HardwareVerseion[] = "2.0.0";
static const char FwVerHead[] = "TWN4/B1.08";
static const char FwVerNum[] = I_NUMBER;
//	static const char FwVerNumP[] = I_NUMBER;
static const char FwVerTypeH[] = "NCF4.06";
static const char FwVerTypeL[] = "NCL4.06";
static const char FwVerName[] = VER_NAME;


static char* VersionAppend(char* buf, char* end, const char* info){
	char* dest = buf;
	const char* src = info;

	while(dest < end && *src){
		*dest = *src;
		dest++; src++;
	}
	
	return dest;
}

int GetVersionHw(char* ver, int size){
	int clen = size;
	
	if(clen > sizeof(HardwareVerseion)){
		clen = sizeof(HardwareVerseion);
	}
	
	memcpy(ver, HardwareVerseion, clen);
	
	return clen;
}

#if 0
int GetVersionSw(char* ver, int size){
	char* vend = ver + size;
	char* v = ver;

	v = VersionAppend(v, vend, FwVerHead);
	v = VersionAppend(v, vend, "/");
	const char* type;

	if(reader.attr & RD_ATTR_HF){
		type = FwVerTypeH;
	}
	else{
		type = FwVerTypeL;
	}
	
	v = VersionAppend(v, vend, type);
	v = VersionAppend(v, vend, "/");
	v = VersionAppend(v, vend, FwVerName);
	v = VersionAppend(v, vend, FwVerNum);
	v = VersionAppend(v, vend, "/");
	
	v = VersionAppend(v, vend, "P");
	
	if(reader.attr & RD_ATTR_SE){
		v = VersionAppend(v, vend, "I");
	}
	
	if(reader.attr & RD_ATTR_BT){
		v = VersionAppend(v, vend, "B");
	}

	return v - ver;
}
#endif 

int GetVersionSw2(char* ver){
	strcpy(ver, FwVerHead);
	strcat(ver, "/");
	
	const char* type;
	if(reader.attr & RD_ATTR_HF){
		type = FwVerTypeH;
	}
	else{
		type = FwVerTypeL;
	}
	
	strcat(ver, type);
	strcat(ver, "/");
	strcat(ver, FwVerName);
	strcat(ver, FwVerNum);

	int vlen = strlen(ver);
	char func[4] = {0};
	int flen = 0;
	
	if(reader.attr & (RD_ATTR_HIDPROX|RD_ATTR_SE|RD_ATTR_BT)){
		if(reader.attr & RD_ATTR_HIDPROX){
			func[flen++] = 'P';
		}
#if 0
		if((reader.attr & (RD_ATTR_SE|RD_ATTR_HIDICLASS))
			== (RD_ATTR_SE|RD_ATTR_HIDICLASS)){
			func[flen++] = 'I';
		}
#endif 
		if(reader.attr & RD_ATTR_SE){
			func[flen++] = 'W';
		}
		if(reader.attr & RD_ATTR_BT){
			func[flen++] = 'B';
		}
	}
	
	if(flen){
		ver[vlen++] = '/';
		for (int i = 0; i < flen; ++i){
			ver[vlen++] = func[i];
		}
	}
	ver[vlen] = 0;
	return vlen;
}


void GetMCUInfo(MCU_Info_T* info){
	memset(info, 0, sizeof(*info));
	
	strcpy((char*)info->Model, FwVerName);
	if(reader.attr & RD_ATTR_SE){
		strcat((char*)info->Model, "Bi");
	}
	else{
		strcat((char*)info->Model, "B");
	}

	strcpy((char*)info->ver, FwVerNum);
}



